﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using MyBet.Web;

namespace MyBet.Helpers
{
    public class LangHelper : INotifyPropertyChanged
    {
        private static MyBet.Lang.Lang mResource = new MyBet.Lang.Lang();
        private static ResourceManager resourceManager = null;
        private static string currentCulture;

        public const string CultureFr = "fr";
        public const string CultureEn = "en";
        public const string DefaultCulture = CultureFr;

        public MyBet.Lang.Lang Resource
        {
            get { return mResource; }
        }

        public static string CurrentCulture
        {
            get { return LangHelper.currentCulture; }
            set { LangHelper.currentCulture = value; }
        }

        /// <summary>
        /// Load resource manager
        /// </summary>
        /// <param name="culture">culture to load</param>
        public static void SetCulture(string culture)
        {
            CurrentCulture = culture.ToLower();
            if (CurrentCulture == DefaultCulture.ToLower())
            {
                resourceManager = new ResourceManager("MyBet.Lang.Lang", typeof(LangHelper).Assembly);
            }
            else
            {
                resourceManager = new ResourceManager("MyBet.Lang.Lang", typeof(LangHelper).Assembly);
            }
        }

        public static string GetString(string label)
        {
            return resourceManager.GetString(label);
        }

        public void ChangeCulture(string culture)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            NotifyPropertyChanged("Resource");
            SetCulture(culture);
        }

        public static IEnumerable<string> GetSecurityQuestions()
        {
            PropertyInfo[] securityInfos;

            if (CurrentCulture==CultureEn)
            { 
                securityInfos = typeof(SecurityQuestions).GetProperties();
            }
            else
            {
                //Default culture
                securityInfos = typeof(SecurityQuestions).GetProperties();
            }

            IEnumerable<string> availableQuestions = from propertyInfo in securityInfos
                                                     where propertyInfo.PropertyType.Equals(typeof(string))
                                                     select (string)propertyInfo.GetValue(null, null);
            return availableQuestions;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion

    }
}
